<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2022-04-19 11:17:49
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-06-01 15:22:57
-->
<template>
  <div>
    <el-dialog
      title="添加团队成员"
      :visible.sync="dialog.joinTeamShow"
      width="30%"
      @close="teamClose"
      :modal="false"
    >
      <el-form
        :model="teamForm"
        ref="teamForm"
        :rules="teamRules"
        label-position="top"
      >
        <el-form-item label="选择团队成员" prop="userIds">
          <el-select
            v-model="teamForm.userIds"
            placeholder="点击选择(多选)"
            multiple
            clearable
          >
            <el-option
              v-for="item in teamOptions"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限">
          <el-radio v-model="teamForm.readonly" label="1">只读</el-radio>
          <el-radio v-model="teamForm.readonly" label="0">读写</el-radio>
        </el-form-item>
        <el-form-item label="同时添加至" v-if="type == 9">
          <el-checkbox-group v-model="teamForm.modules">
            <template v-for="(item, index) in addList">
              <el-checkbox :key="index" :label="item.value">
                {{ item.label }}
              </el-checkbox>
            </template>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="有效时间">
          <el-select v-model="teamForm.effValue" @change="timeChange">
            <el-option
              v-for="item in effOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-date-picker
            v-model="teamForm.expire"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            v-if="timeShow"
            style="margin-left: 20px"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialog.joinTeamShow = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addList, insert } from "@/api/team";
import { distriBution } from "@/api/registration";
export default {
  props: {
    dialog: {
      type: Object,
      required: true,
    },
    drawerData: {
      type: Object,
      required: true,
    },
    type: {
      type: Number,
      required: true,
    },
    entities: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      timeShow: false,
      teamForm: {
        module: this.type,
        userIds: [],
        entities: this.entities,
        readonly: "0",
        effValue: 1,
        expire: "",
        modules: [],
      },
      addList: [
        { value: 13, label: "联系人" },
        { value: 12, label: "商机" },
        { value: 60, label: "合同" },
      ],
      teamRules: {
        userIds: [
          { required: true, message: "请选择团队成员", trigger: "change" },
        ],
      },
      teamOptions: [],
      effOptions: [
        {
          value: 1,
          label: "不限",
        },
        {
          value: 2,
          label: "截止到",
        },
      ],
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    // 获取团队候选列表
    getUserList() {
      distriBution().then((res) => {
        this.teamOptions = res.data;
      });
    },
    // 关闭新增团队成员
    teamClose() {
      this.dialog.joinTeamShowEject = false;
      this.teamForm = {
        userIds: [],
        readonly: "0",
        effValue: 1,
        expire: "",
        modules: [],
      };
      this.$$parent.entities=[]
      this.timeShow = false;
    },
    // 确定增加
    onSubmit() {
      this.$refs["teamForm"].validate((valid) => {
        if (!valid) return;
        insert(this.teamForm).then((res) => {
          this.$message.success(res.msg);
          this.$parent.getList();
          this.dialog.joinTeamShow = false;
        });
      });
    },
    timeChange(val) {
      if (val == 1) {
        this.timeShow = false;
      } else if (val == 2) {
        this.timeShow = true;
      }
    },
  },
};
</script>

<style lang="less" scoped>
</style>